package k.daniel.databindingdemo;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import k.daniel.databindingdemo.bean.UserBean;
import k.daniel.databindingdemo.databinding.ActivitySimpleListViewBinding;
import k.daniel.databindingdemo.databinding.LayoutListviewItemBinding;
import k.daniel.databindingdemo.model.UserModel;

public class SimpleListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivitySimpleListViewBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_simple_list_view);
        binding.listView.setAdapter(new MyAdpater(new UserModel().getUsers()));

    }
    class MyAdpater extends BaseAdapter{
        List<UserBean> userBeans =new ArrayList<>();

        public MyAdpater(List<UserBean> userBeans) {
            this.userBeans = userBeans;
        }

        @Override
        public int getCount() {
            return userBeans.size();
        }

        @Override
        public Object getItem(int i) {
            return userBeans.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null)
            {
                LayoutListviewItemBinding binding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.layout_listview_item,viewGroup,false);
                view=binding.getRoot();
                view.setTag(binding);
            }
            LayoutListviewItemBinding binding= (LayoutListviewItemBinding) view.getTag();
            binding.setUser((UserBean) getItem(i));
            binding.executePendingBindings();
            return view;
        }
    }
}
